package service.impl;

import java.util.ArrayList;
import java.util.List;

import dao.CartDao;
import dao.DishDao;
import dao.impl.CartDaoImpl;
import dao.impl.DishDaoImpl;
import po.Cart;
import po.Dish;
import service.CartService;

public class CartServiceImpl implements CartService{

	@Override
	public int addCart(Cart cart, int cId) {
		CartDao cartDaoImpl = new CartDaoImpl();
		String cartId = cart.getCartId();
		int dId= cart.getdId();
		int num = cart.getdNum();	
		String cdId = cart.getCdId();
		int m = cartDaoImpl.addCart(cartId, cId);	 
		int n = cartDaoImpl.addCartDish(cdId,cartId, num, dId);
		if(m == 1){
			if(n == 1){
				return 1;
			}else{
				System.out.println("add cartDish fail");
			}
		}else{
			System.out.println("add cart failed");
		}
		return 0;
	}

	@Override
	public int delCart(String cartId) {
		CartDao cartDaoImpl = new CartDaoImpl();
		int m = cartDaoImpl.delCart(cartId);
		int n = cartDaoImpl.delCartDish(cartId);
		if(m == 1){
			if(n == 1){
				return 1;
			}else{
				System.out.println("del cartDish failed");
			}
		}else{
			System.out.println("del cart failed");
		}
		return 0;
	}
	

	@Override
	public int updateCart(String cdId, int num) {
		CartDao cartDaoImpl = new CartDaoImpl();
		int m = cartDaoImpl.updateCart(cdId, num);
		if(m == 1){
			return 1;
		}else{
			System.out.println("update cartDish failed");
		}
		return 0;
	}

	@Override
	public List<Dish> queryCart(int cId) {
		CartDao cartDaoImpl = new CartDaoImpl();
		DishDao dishDaoImpl = new DishDaoImpl();
		List<String> cartIdList = new ArrayList<String>();
		List<Cart> cartList = new ArrayList<Cart>();
		List<Dish> dishList = new ArrayList<Dish>();
		cartIdList = cartDaoImpl.queryCart(cId);
		for(String cartId:cartIdList){
			Cart cart = cartDaoImpl.queryCartDish(cartId);
			cartList.add(cart);
			System.out.println(cart);
		}
		for(Cart cart:cartList){
			int dId = cart.getdId();
			Dish dish = dishDaoImpl.queryADish(dId);
			dish.setCartId(cart.getCartId());
			dish.setCdId(cart.getCdId());
			dish.setNum(cart.getdNum());
			dishList.add(dish);
		}
		return dishList;
	}

	
	
}
